<?php

namespace app\api\service;

use think\facade\Db;

class GroupActivityService
{
    public static function getHomeGroupActivityImage($store_id)
    {
       $list =  Db::name('group_activity')
                            ->field('id,activity_cover')
                            ->where(['status' => 1,'store_id' => $store_id])
                            ->withAttr('activity_cover',function ($value){
                                return request()->domain() . $value;
                            })->whereBetweenTimeField('start_time','end_time')
                            ->select();
       
       return $list;

    }

    /**
     *  获取拼团商品
     * */
    public static function getActivityGoods($group_activity_id,$is_page = true)
    {
        $query = Db::name('group_goods')
                   ->alias('a')
                   ->field('a.id,a.goods_name,a.main_image,a.sell_price,a.group_price')
                   ->join('group_activity_goods b','a.id=b.group_goods_id')
                   ->withAttr('main_image',function ($value){
                        return request()->domain() . $value;
                   })->where('b.group_activity_id',$group_activity_id)
                   ->order(['a.sort'=>'asc','a.total_sale'=>'desc','a.id' => 'desc']);

        if ($is_page){
            $goods = $query->paginate(10);
        } else {
            $goods = $query->limit(6)->select();
        }



        return $goods;
    }


    /**
     *  获取拼团商品
     * */
    public static function getHotActivityGoods($store_id)
    {
        $goods = Db::name('group_goods')
            ->alias('a')
            ->field('a.id,a.goods_name,a.main_image,a.sell_price,a.group_price,c.activity_title')
            ->join('group_activity_goods b','a.id=b.group_goods_id')
            ->join('group_activity c','b.group_activity_id=c.id')
            ->where('a.store_id',$store_id)
            ->withAttr('main_image',function ($value){
                return request()->domain() . $value;
            })->whereBetweenTimeField('c.start_time','c.end_time')
            ->order(['a.sort'=>'asc','a.total_sale'=>'desc','a.id' => 'desc'])
            ->limit(10)
            ->select();

        return $goods;
    }


    /**
     * 获取拼团活动及相应商品
     * */
    public static function getActivityList($store_id)
    {

       $groupActivityList = Db::name('group_activity')
                              ->field('id,activity_title,activity_cover,start_time,end_time,prepayment_amount')
                              ->whereBetweenTimeField('start_time','end_time')
                              ->withAttr('activity_cover',function ($value){
                                    return request()->domain() . $value;
                              })->where(['status' => 1,'store_id' => $store_id,'delete_time'=> 0])
                              ->select();

       $groupActivityInfo = [];
       foreach ($groupActivityList as $item){
           $item['goods'] = static::getActivityGoods($item['id'],false);
           $groupActivityInfo[] = $item;
       }


       return $groupActivityInfo;
    }


    /**
     *  获取首页拼团活动详情
     * */
    public static function findActivityGroupGoods($group_goods_id)
    {
            $groupGoods = Db::name('group_goods')
                            ->alias('a')
                            ->field('a.id,a.goods_name,a.main_image,a.goods_images,a.spec_type,a.market_price,a.sell_price,a.group_price,
                            a.is_single_buy,a.goods_attr,b.group_activity_id,d.content,a.status,c.activity_title,c.rule_content,c.end_time,c.prepayment_amount,c.status as activity_status')
                            ->join('group_activity_goods b','a.id=b.group_goods_id')
                            ->join('group_activity c','c.id=b.group_activity_id')
                            ->join('group_goods_content d','a.id=d.group_goods_id')
                            ->withAttr('main_image',function ($value){
                                return request()->domain() . $value;
                            })->withAttr('goods_attr',function ($value){
                                if(empty($value)){
                                    return [];
                                }
                                return json_decode($value,true);
                            })->withAttr('goods_images',function ($value){
                                $imgs = [];
                                $arr = json_decode($value,true);
                                foreach ($arr as $v){
                                    $imgs[] = request()->domain() . $v;
                                }
                                return $imgs;
                            })->where('a.id',$group_goods_id)
                            ->find();
            $items = [];
            if ($groupGoods['spec_type']==2){
                $items = Db::name('group_goods_item')
                           ->field('id,attr,image,market_price,sell_price,group_price,stock')
                           ->withAttr('image',function ($value){
                                return request()->domain() . $value;
                           })->where('group_goods_id',$groupGoods['id'])
                           ->select();
            }

            $groupGoods['items'] = $items;

            return $groupGoods;
    }
}